package com.sunfeng.chap2.p13;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @Author : 孙峰
 * @Description:  共享锁不能自己写   注意是加锁的位置不能自己写
 * @Date : 2021/10/9  16:35
 */
public class Test13_6 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("d:\\nio\\a.txt", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();

        fileChannel.lock(1, 2, true);
        fileChannel.write(ByteBuffer.wrap("123456".getBytes(StandardCharsets.UTF_8)));
    }
}
